import random
import tkinter as tk
import time

window = tk.Tk()

start = 0
end = 0
# 结果集
result = []
# 答案
entryList = []
# 标签
labelList = []
count = 0


def createMenu(window, title, size):
    window.title(title)
    window.geometry(size)

    # 初始化答题界面
    l = tk.Label(window, text="欢迎使用四则运算测试题 下面选择运算符出题", width="400", height="10", font=("Arial", 18),
                 bg="skyblue")
    badd = tk.Button(window, text="+", width="10", height="2", bg="pink", command=add)
    breduce = tk.Button(window, text="-", width="10", height="2", bg="pink", command=reduce)
    bmul = tk.Button(window, text="*", width="10", height="2", bg="pink", command=mul)
    bdiv = tk.Button(window, text="/", width="10", height="2", bg="pink", command=div)

    l.pack()
    badd.pack()
    breduce.pack()
    bmul.pack()
    bdiv.pack()

    window.mainloop()


# 清空列表
def listClear():
    result.clear()
    labelList.clear()
    entryList.clear()


# 结果窗口
def resultWindow(count):
    window = tk.Tk()
    window.title("结果")
    window.geometry("200x200")
    tk.Label(window, text="恭喜你 答对了{} 题".format(count)).pack()
    tk.Label(window, text="正确率为 {}%".format((count / 5) * 100)).pack()
    tk.Label(window, text="用时 {} s".format(end - start)).pack()


# 判断提交的正确性
def submit():
    # 统计做题时间
    global end
    end = time.time()
    count = 0
    # 判断正确题目个数
    for i in range(0, 5):
        if result[i] == int(entryList[i].get()):
            count += 1
    resultWindow(count)


def createCount(title, id, calformat):
    # 清空列表
    listClear()
    global start
    start = time.time()
    window = tk.Tk()
    window.title(title)
    window.geometry("700x700")
    b = tk.Button(window, text="提交答案", command=submit)
    for j in range(0, 5):
        x = random.randint(1, 10)
        y = random.randint(1, 10)
        # 存答案
        if id == 1:
            result.append(x + y)
        elif id == 2:
            result.append(x - y)
        elif id == 3:
            result.append(x * y)
        else:
            result.append(x // y)
        # 循环生成Lable Entry
        labelList.append(tk.Label(window, text=calformat.format(x, y)))
        entryList.append(tk.Entry(window, show=None))
    list_size = len(labelList)
    # 循环创建
    for i in range(0, list_size):
        labelList[i].pack()
        entryList[i].pack()

    b.pack()
    tk.mainloop()


# 加法
def add():
    createCount("加法", 1, "{} + {}")


# 减法
def reduce():
    createCount("减法", 2, "{} - {}")


# 乘法
def mul():
    createCount("乘法", 3, "{} * {}")


# 除法
def div():
    createCount("除法", 4, "{} // {}")


# 创建主界面
createMenu(window, "四则运算", "1000x1000")
